package com.fyp.ptma.security;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

public class LoginSuccessHandler implements AuthenticationSuccessHandler,
		AuthenticationFailureHandler {

	public void onAuthenticationSuccess(HttpServletRequest request,
			HttpServletResponse response, Authentication auth)
			throws IOException, ServletException {
		request.getSession(true).setAttribute("user", auth.getPrincipal());
		request.setAttribute("user", auth.getPrincipal());

		PrintWriter writer = response.getWriter();
		writer.write("secure/applications.html");
		writer.flush();
		writer.close();
	}

	public void onAuthenticationFailure(HttpServletRequest request,
			HttpServletResponse response, AuthenticationException auth)
			throws IOException, ServletException {
		// TODO Auto-generated method stub

		request.getSession(true).setAttribute("error", auth.getMessage());
		request.setAttribute("error", auth.getMessage());

	}

}
